package bb.lanxing.calc.data;

import com.github.mikephil.charting.utils.Utils;
import bb.lanxing.manager.RemoteSharedPreference;
import bb.lanxing.manager.SPConstant;
import bb.lanxing.model.database.Workout;
import bb.lanxing.util.DistanceUtil;
import bb.lanxing.util.DurationUtil;

public class SourcePointPair {
    private static final int MIN_DISTANCE_INTERNAL = 4;
    private double acceleration;
    private SourcePoint curSourcePoint;
    private double distance;
    private long duration;
    private SourcePoint lastDistancePoint;
    private SourcePoint lastSourcePoint;
    private double speed;
    private double validDistance;

    public void update(SourcePoint sourcePoint) {
        SourcePoint sourcePoint2 = this.curSourcePoint;
        this.lastSourcePoint = sourcePoint2 != null ? sourcePoint2.copy(this.lastSourcePoint) : sourcePoint.copy(this.lastSourcePoint);
        SourcePoint copy = sourcePoint.copy(this.curSourcePoint);
        this.curSourcePoint = copy;
        double d = DistanceUtil.get(copy, this.lastSourcePoint);
        double withCadence = DistanceUtil.getWithCadence(this.curSourcePoint, this.lastSourcePoint);
        int i = RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING);//3
        if ((i == Workout.TYPE_TRAINING)
            || (i == Workout.TYPE_INDOOR_BIKE)
            || (i == Workout.TYPE_BIKE_TRAINER)
            || (i == Workout.TYPE_ROWER)
            || (i == Workout.TYPE_CROSS_TRAINER)){
            d = withCadence;
        }

        this.distance = d;
        long j = DurationUtil.get(this.curSourcePoint.getTimestamp(), this.lastSourcePoint.getTimestamp());
        this.duration = j;
        if (j > 0) {
            this.speed = this.distance / j;
            this.acceleration = (this.curSourcePoint.getSpeed() - this.lastSourcePoint.getSpeed()) / this.duration;
        } else if (j < 0) {
            this.duration = 0L;
        }
        if ((i == Workout.TYPE_TRAINING)
            || (i == Workout.TYPE_INDOOR_BIKE)
            || (i == Workout.TYPE_BIKE_TRAINER)
            || (i == Workout.TYPE_ROWER)
            || (i == Workout.TYPE_CROSS_TRAINER)){
            this.validDistance = this.distance;
            return;
        }
        SourcePoint sourcePoint3 = this.lastDistancePoint;
        if (sourcePoint3 == null) {
            this.lastDistancePoint = sourcePoint.copy(null);
            return;
        }
        double d2 = DistanceUtil.get(this.curSourcePoint, sourcePoint3);
        this.validDistance = d2;
        if (d2 > 4.0d) {
            this.lastDistancePoint = this.curSourcePoint.copy(this.lastDistancePoint);
        } else {
            this.validDistance = Utils.DOUBLE_EPSILON;
        }
    }

    public void release() {
        this.lastDistancePoint = null;
        this.lastSourcePoint = null;
        this.curSourcePoint = null;
        this.distance = Utils.DOUBLE_EPSILON;
        this.duration = 0L;
        this.speed = Utils.DOUBLE_EPSILON;
        this.acceleration = Utils.DOUBLE_EPSILON;
        this.validDistance = Utils.DOUBLE_EPSILON;
    }

    public double getDistance() {
        return this.distance;
    }

    public long getDuration() {
        return this.duration;
    }

    public double getSpeed() {
        return this.speed;
    }

    public double getAcceleration() {
        return this.acceleration;
    }

    public SourcePoint getLastSourcePoint() {
        return this.lastSourcePoint;
    }

    public SourcePoint getCurSourcePoint() {
        return this.curSourcePoint;
    }

    public double getValidDistance() {
        return this.validDistance;
    }
}
